﻿using App.Extension;
using Simple.BasicNet.Core;
using Simple.BasicNet.Core.Atrributes;
using Simple.BasicNet.Core.Handle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 App.Pipeline
 * 接口名称 VerifyPipeline
 * 开发人员：11920
 * 创建时间：2023/4/14 11:00:58
 * 描述说明：
 * 更改历史：
 * 校验包头包尾管道
 * *******************************************************/
namespace App.Pipeline
{
	internal class VerifyPipeline : IPipeline
	{
		[Autowired]
		public ILogger logger { get; set; }	
		public bool Process(IContext context)
		{

			if (!CheckHeadAndTail(context))
			{
				context.Send(IDTProtocolPack.ErrorReply(eReceivePackType.CheckHeadAndTailFail));
				logger.Error("数据包头或者包尾错误!");
				return false;
			}

			if (!IDTProtocolPack.CalculateChecksum(context))
			{
				context.Send(IDTProtocolPack.ErrorReply(eReceivePackType.CheckSumFail));
				logger.Error("CRC 校验错误!");
				return false;
			}
			logger.Info($"commanid:{context.CurrentPack.CommandID} data:{string.Join(" ",context.CurrentPack.ReceiveBuffer.Select(x => x.ToString()).ToArray())}complete{context.CurrentPack.Complete} len{context.CurrentPack.Length}");
			return true;
		}

		private bool CheckHeadAndTail(IContext context)
		{
			return IDTProtocolPack.CheckHeadAndTail(context);
		}


	
	}
}
